#ifndef _RTC_H_
#define _RTC_H_

#include "pulpino.h"
#include <stdint.h>

#define PARAM_RTC_Sec_RATE(Sec_rate)         (Sec_rate  <= (0x3B) && Sec_rate  >= (0x00))
#define PARAM_RTC_Min_RATE(Min_rate)         (Min_rate  <= (0x3B) && Min_rate  >= (0x00))
#define PARAM_RTC_Hour_RATE(Hour_rate)       (Hour_rate <= (0x17) && Hour_rate >= (0x00))
#define PARAM_RTC_Year_RATE(Year_rate)       (Year_rate <= (0x63) && Year_rate >= (0x00))
#define PARAM_RTC_Mon_RATE(Mon_rate)         (Mon_rate  <= (0x0C) && Mon_rate  >= (0x01))
#define PARAM_RTC_Day_RATE(Day_rate)         (Day_rate  <= (Day_rate_max[Mon_rate]) && (Day_rate_max[Mon_rate])  >= (0x01))
#define PARAM_RTC_Week_RATE(Week_rate)       (Week_rate <= (0x07) && Week_rate >= (0x01))
#define PARAM_RTC_ADDR(RTCx)                 (RTCx==UCCHIP_RTC)

typedef struct{
	uint32_t TIM0;
	uint32_t TIM1;
}RTC_DATA,*PRTC_DATA;

typedef enum{
    ENSET_ENABLE     = 1,
    ENSET_DISENABLE  = 0
}RTC_ALARM_EN;

typedef enum{
    ENYEAR_ENABLE     = 0,
    ENYEAR_DISENABLE  = 1
}YEAR_ALM_MASK;

typedef enum{
    ENMON_ENABLE      = 0,
    ENMON_DISENABLE   = 1
}MON_ALM_MASK;

typedef enum{
    ENDAY_ENABLE      = 0,
    ENDAY_DISENABLE   = 1
}DAY_ALM_MASK;

typedef enum{
    ENWEEK_ENBLE      = 0,
    ENWEEK_DISENABLE  = 1
}WEEK_ALM_MASK;

typedef enum{
    ENHOUR_ENABLE     = 1,
    ENHOUR_DISENABLE  = 0
}HOUR_ALM_MASK;

typedef enum{
    ENMIN_ENABLE     = 1,
    ENMIN_DISENABLE  = 0
}MIN_ALM_MASK;

typedef enum{
    ENSEC_ENABLE     = 1,
    ENSEC_DISENABLE  = 0
}SEC_ALM_MASK;

typedef struct{
    uint8_t Hour_rate;
    uint8_t Min_rate;
    uint8_t Sec_rate;
    uint16_t Year_rate;
    uint8_t Mon_rate;
    uint8_t Day_rate;
    uint8_t Week_rate;
}RTC_CFG_Type;

typedef struct{
    RTC_ALARM_EN    Enset;
    YEAR_ALM_MASK   Enyear;
    MON_ALM_MASK    Enmon;
    DAY_ALM_MASK    Enday;
    WEEK_ALM_MASK   Enweek;
    HOUR_ALM_MASK   Enhour;
    MIN_ALM_MASK    Enmin;
    SEC_ALM_MASK    Ensec;    
}RTC_ACFG_Type;


#define TLR_ENABLE_MASK      0x04
#define TLR_DISENABLE_MASK   0x00
#define TSR_ENABLE_MASK      0x02
#define TSR_DISENABLE_MASK   0x00

void set_rtc(RTC_TypeDef *RTCx, RTC_CFG_Type *RTC_ConfigStruct);
void set_rtc_alm(RTC_TypeDef *RTCx, RTC_ACFG_Type *RTC_AlmCfg, RTC_CFG_Type *RTC_Alm_time);

uint16_t get_year(RTC_TypeDef *RTCx);
uint8_t get_month(RTC_TypeDef *RTCx);
uint8_t get_day(RTC_TypeDef *RTCx);
uint8_t get_week(RTC_TypeDef *RTCx);
uint8_t get_hour(RTC_TypeDef *RTCx);
uint8_t get_min(RTC_TypeDef *RTCx);
uint8_t get_sec(RTC_TypeDef *RTCx);
void get_now_time(RTC_TypeDef *RTCx, RTC_CFG_Type *RTC_ConfigStruct);
#endif